package cn.itcast.service.cargo.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
public class PackingServiceImpl implements PackingService {

    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportService exportService;
    @Override
    public int deleteByPrimaryKey(String packingListId) {
        return packingDao.deleteByPrimaryKey(packingListId);
    }

    @Override
    public int insert(Packing record) {
        return 0;
    }

    @Override
    public int insertSelective(Packing record) {
        return packingDao.insertSelective(record);
    }

    @Override
    public List<Packing> selectByExample(PackingExample example) {
        return packingDao.selectByExample(example);
    }

    @Override
    public Packing selectByPrimaryKey(String packingListId) {
        return packingDao.selectByPrimaryKey(packingListId);
    }

    @Override
    public int updateByPrimaryKeySelective(Packing record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Packing record) {
        return 0;
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, PackingExample example) {
        PageHelper.startPage(pageNum,pageSize);
        List<Packing> list = packingDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void save(Packing packing) {
        packing.setPackingListId(IdUtil.simpleUUID());
        packing.setCreateTime(new Date());
        packing.setInvoiceDate(new Date());
        packing.setState(0);
        String[] exportArray = packing.getExportIds().split(",");
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportArray));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        String customerContract = "";
        for (Export export : exportList) {
            customerContract += (export.getLcno()+",");
            export.setState(3);
            exportService.update(export);
        }
        packing.setExportNos(customerContract);
        packingDao.insertSelective(packing);
    }




    @Override
    public void update(Packing packing) {
        packingDao.updateByPrimaryKeySelective(packing);
    }
}
